package com.example.hanchen.yongkanggy.http;

import android.util.Log;
import android.widget.Toast;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.hanchen.yongkanggy.entity.StatusCode;
import com.example.hanchen.yongkanggy.entity.verificationimgEntity;
import com.example.hanchen.yongkanggy.home.MyApplication;

import java.io.File;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class UserViewModel extends ViewModel {
    private UserRepository userRepository;

    /*跳转到新页面重新登录*/
    public int TOKEN_NOT_NELL = 100;/*令牌为空*/
    public int TOKEN_OVERDUE = 101;
    public int USER_DISABLED = 102;
    public int SUCCESS = 200;/*成功*/
    /*重要的提示弹窗*/
    public int ERROR_WARNING = 302;/*操作失败*/

    public int ERROR_DEFAULT = 301;/*操作失败*/
    public int ERROR_FREQUENTLY = 303;/*重复操作*/

    private MutableLiveData<StatusCode> statuscode = new MutableLiveData<>();
    private MutableLiveData<verificationimgEntity> verificationimgEntity = new MutableLiveData<>();


    public UserViewModel() {
        userRepository = new UserRepository();
    }

    /*code 统一处理*/
    private void codeError(int code, String message) {
        if (code == TOKEN_NOT_NELL || code == TOKEN_OVERDUE || code == USER_DISABLED) {/*跳转到新页面重新登录*/
            //code 异常 \ token失效 跳转到登录界面
//            HomeActivity.finishActivity();
        } else if (code == ERROR_WARNING || code == ERROR_FREQUENTLY || code == ERROR_DEFAULT) {/*操作失败*/
            Toast.makeText(MyApplication.getContext(), message, Toast.LENGTH_SHORT).show();
        }
    }


    public LiveData<StatusCode> clientuserlogin(String account, String password, String code,String parameterSign) {
        userRepository.clientuserlogin(account, password,code, parameterSign)
                .subscribe(new Observer<StatusCode>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(StatusCode user) {
                        if (user.getCode() == SUCCESS) {
                            statuscode.setValue(user);
                            Log.i("JFEIOJFIEJIFIEOF","onNexta");
                        } else {
                            codeError(user.getCode(), user.getMsg());
                            Log.i("JFEIOJFIEJIFIEOF","onNextb");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
        return statuscode;
    }


    public LiveData<verificationimgEntity> verificationimg(File file, String moduleName) {
        userRepository.verificationimg(file, moduleName)
                .subscribe(new Observer<verificationimgEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(verificationimgEntity user) {
                        if (user.getCode() == SUCCESS) {
                            verificationimgEntity.setValue(user);
                            Log.i("JFEIOJFIEJIFIEOF","onNexta");
                        } else {
                            codeError(user.getCode(), user.getMsg());
                            Log.i("JFEIOJFIEJIFIEOF","onNextb");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
        return verificationimgEntity;
    }



}